package com.chenglu.android.order.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chenglu.android.R;
import com.chenglu.android.common.utils.PxUtils;
import com.chenglu.android.common.utils.Utils;
import com.chenglu.android.order.OrderActivity;
import com.chenglu.android.order.TextStatusUtils;
import com.chenglu.android.order.bean.OrderInfoBean;
import com.chenglu.android.orderinfo.OrderInfoActivity;
import com.chenglu.android.orderinfo.OrderInfoUtils;


import org.w3c.dom.Text;

import java.util.List;

/**
 * @author: lyj
 * @Created: by Copyright 2023 3 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：
 */
public class AllOrderAdapter extends BaseQuickAdapter<OrderInfoBean.Data.Records, BaseViewHolder> {

    private Context mContext;


    public AllOrderAdapter(Context context, @Nullable List<OrderInfoBean.Data.Records> data) {
        super(R.layout.item_order, data);
        this.mContext = context;
    }


    @Override
    protected void convert(@NonNull BaseViewHolder helper, OrderInfoBean.Data.Records item) {

        helper.setText(R.id.tv_order_no, item.getOrderNo());

        //店铺名称

        TextView tv_shop_name = helper.getView(R.id.tv_shop_name);
        tv_shop_name.setText(item.getShopsName());

        ImageView ivshop =  helper.getView(R.id.iv_shop);

        RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.seller_icon )
                .error(R.drawable.seller_icon)
                .transform(new RoundedCorners(PxUtils.dp2px(4)));
         Glide.with(mContext).load(item.getLogoImage()).apply(options).into(ivshop);

        RelativeLayout rlOrderComment = helper.getView(R.id.rl_order_comment);
        rlOrderComment.setVisibility(View.GONE);

        //总价格
        TextView tvCartPrice = helper.getView(R.id.tv_cart_price);

        tvCartPrice.setText("¥" + Utils.formatZeroNumber(item.getOrderActuallyPaid()));

        TextView tvCartGoodsNum = helper.getView(R.id.tv_cart_goodsNum);

        int num = 0;
        for (int i = 0; i <item.getOrderDetails().size() ; i++) {
             num += item.getOrderDetails().get(i).getGoodsAmount();
        }
        tvCartGoodsNum.setText("共"+num+"件");

        TextView tvOrderStatus = helper.getView(R.id.tv_order_status);

        TextView btn1 = helper.getView(R.id.tv_order_btn1);
        TextView btn2 = helper.getView(R.id.tv_order_btn2);
        TextView btn3 = helper.getView(R.id.tv_order_btn3);

        //需要判断类型的 订单总价	支付类型：1支付宝 2微信 3余额(红包) 4积分
        helper.setText(R.id.tv_order_sum, "合计：¥" + Utils.formatZeroNumber(item.getOrderActuallyPaid()));

        //设置文字 待收货等状态
        TextStatusUtils.setHeadText(item.getShopsTypeId(),item.getStatus(),0,item.getExtStatus(),tvOrderStatus);

        // 设置订单按钮的颜色
        TextStatusUtils.setBtn2Colors(mContext,btn2,item.getOrderType());

        //判断底部按钮的文字
        rlOrderComment.setVisibility(View.VISIBLE);
        TextStatusUtils.setfooterBtn(mContext,item.getShopsTypeId(),item.getStatus(),item.getExtStatus(),btn1,btn2,btn3);
       // 订单状态 0、已取消 1、待支付 2、待发货 3、待收货 4、待评价 5、已评价
//        switch (item.getStatus()) {
//            //订单状态：0已取消,1待付款,2待发货,3待收货,4已评价,-1取消交易,-2售后申请 -3已退款
//            case 0:
////                helper.setText(R.id.tv_order_status, "已取消");
//                rlOrderComment.setVisibility(View.VISIBLE);
//
//                helper.setGone(R.id.tv_order_btn3,false);
//                //按钮1 删除订单
//                helper.setText(R.id.tv_order_btn1, mContext.getResources().getString(R.string.order_delete))
//                        .setVisible(R.id.tv_order_btn1, true)
//                        .addOnClickListener(R.id.tv_order_btn1);
//                //按钮2 再次购买
//                helper.setText(R.id.tv_order_btn2, mContext.getResources().getString(R.string.order_buy_again))
//                        .setVisible(R.id.tv_order_btn2, true)
//                        .addOnClickListener(R.id.tv_order_btn2);
//                break;
//            case 1:
////                helper.setText(R.id.tv_order_status, "待支付");
//
//                rlOrderComment.setVisibility(View.VISIBLE);
//                //按钮1 取消订单
//                helper.setText(R.id.tv_order_btn1, mContext.getResources().getString(R.string.order_cancel))
//                        .setVisible(R.id.tv_order_btn1, true)
//                        .addOnClickListener(R.id.tv_order_btn1);
//                //按钮2 立即支付
//                helper.setText(R.id.tv_order_btn2, mContext.getResources().getString(R.string.order_immediate_payment))
//                        .setVisible(R.id.tv_order_btn2, true)
//                        .addOnClickListener(R.id.tv_order_btn2);
//
//
//                break;
//            case 2:
////                if(item.getShopsTypeId())
////                helper.setText(R.id.tv_order_status, "待发货");
//
//                rlOrderComment.setVisibility(View.VISIBLE);
//                //隐藏 按钮 1 2
//                helper.setGone(R.id.tv_order_btn1,false);
//                helper.setGone(R.id.tv_order_btn2,false);
//                //按钮3 显示3 //联系客服
//                helper.setText(R.id.tv_order_btn3, mContext.getResources().getString(R.string.order_contract_custom))
//                        .setVisible(R.id.tv_order_btn3, true)
//                        .addOnClickListener(R.id.tv_order_btn3);
//                break;
//            case 3:
////                helper.setText(R.id.tv_order_status, "待收货");
//                rlOrderComment.setVisibility(View.VISIBLE);
//
//                //按钮1 查看物流
//                helper.setText(R.id.tv_order_btn1, mContext.getResources().getString(R.string.order_look_logistics))
//                        .setVisible(R.id.tv_order_btn1, true)
//                        .addOnClickListener(R.id.tv_order_btn1);
//                //按钮2 确认收货
//                helper.setText(R.id.tv_order_btn2, mContext.getResources().getString(R.string.order_confirm_receipt))
//                        .setVisible(R.id.tv_order_btn2, true)
//                        .addOnClickListener(R.id.tv_order_btn2);
//                //按钮3 显示3 //联系客服
//                helper.setText(R.id.tv_order_btn3, mContext.getResources().getString(R.string.order_contract_custom))
//                        .setVisible(R.id.tv_order_btn3, true)
//                        .addOnClickListener(R.id.tv_order_btn3);
//
//                break;
//            case 4:
//                rlOrderComment.setVisibility(View.VISIBLE);
//
////                helper.setText(R.id.tv_order_status, "待评价");
//
//                //按钮1 查看物流
//                helper.setText(R.id.tv_order_btn1, mContext.getResources().getString(R.string.order_look_logistics))
//                        .setVisible(R.id.tv_order_btn1, true)
//                        .addOnClickListener(R.id.tv_order_btn1);
//                //按钮2 评价
//                helper.setText(R.id.tv_order_btn2, mContext.getResources().getString(R.string.order_comment_message))
//                        .setVisible(R.id.tv_order_btn2, true)
//                        .addOnClickListener(R.id.tv_order_btn2);
//
//                helper.setText(R.id.tv_order_btn3, mContext.getResources().getString(R.string.order_contract_custom))
//                        .setVisible(R.id.tv_order_btn3, true)
//                        .addOnClickListener(R.id.tv_order_btn3);
//
//
//                break;
//
//            case 5:
//                rlOrderComment.setVisibility(View.VISIBLE);
//                helper.setText(R.id.tv_order_btn3, mContext.getResources().getString(R.string.order_contract_custom))
//                        .setVisible(R.id.tv_order_btn3, true)
//                        .addOnClickListener(R.id.tv_order_btn3);
//
//                break;
//
//            default:
//                helper.setText(R.id.tv_order_status, "已完成");
//                tvOrderStatus.setTextColor(mContext.getResources().getColor(R.color.home_new_color));
//                break;
//
//
//        }


        helper.addOnClickListener(R.id.rl_order_item);
        //三个按钮的设置
        helper.addOnClickListener(R.id.tv_order_btn1);
        helper.addOnClickListener(R.id.tv_order_btn2);
        helper.addOnClickListener(R.id.tv_order_btn3);



        RecyclerView recyclerView = helper.getView(R.id.rv_order_list);

        if(item.getOrderDetails().size()==1){
            AllOrderItemAdapter mItemAdapter = new AllOrderItemAdapter(mContext, R.layout.item_order_all, item.getOrderDetails());

            recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
            recyclerView.setAdapter(mItemAdapter);

            mItemAdapter.notifyDataSetChanged();

            mItemAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    OrderInfoUtils.jumpOrderInfoActivity(mContext,item);

                }
            });

            mItemAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
                @Override
                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                    switch (view.getId()) {
                        case R.id.rv_cart_goosImg:
////                        ToastUtils.show("跳转商品详情");
//                        Intent intent = new Intent(context, HomeInfoActivity.class);
//                        intent.putExtra("id", mItemAdapter.getData().get(position).getOrderDetail().getId());
//                        LogUtils.e("id----" + mItemAdapter.getData().get(position).getOrderDetail().getId());
//                        context.startActivity(intent);
                            break;

                    }
                }
            });

        }else if(item.getOrderDetails().size()>1) {

            AllOrderItemListAdapter orderItemListAdapter = new AllOrderItemListAdapter(mContext, item.getOrderDetails());


            LinearLayoutManager linearLayoutManager=new LinearLayoutManager(mContext,RecyclerView.HORIZONTAL,false){
                //禁止竖向滑动 RecyclerView 为垂直状态（VERTICAL）
//                @Override
//                public boolean canScrollVertically() {
//                    return false;
//                }
                //禁止横向滑动 RecyclerView 为水平状态（HORIZONTAL）
            @Override
            public boolean canScrollHorizontally() {
                return false;
            }
            };
            recyclerView.setLayoutManager(linearLayoutManager);
            recyclerView.setAdapter(orderItemListAdapter);
            orderItemListAdapter.notifyDataSetChanged();

            orderItemListAdapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                   OrderInfoUtils.jumpOrderInfoActivity(mContext,item);

                }
            });
        }










    }

//    private void jumpOrderInfoActivity(OrderInfoBean.Data.Records item) {
//        Intent intent = new Intent(mContext, OrderInfoActivity.class);
//        Bundle bundle = new Bundle();
//        bundle.putSerializable(OrderInfoActivity.GOODSRECORDS, item);
//        intent.putExtra(OrderInfoActivity.ORDERID, item.getId());
//        intent.putExtra(OrderInfoActivity.ORDERNO, item.getOrderNo());
//        intent.putExtras(bundle);
//        mContext.startActivity(intent);
//    }

}
